#include "compile_and_run.hpp"
#include "../common/httplib.h"

using namespace httplib;
using namespace wjh_compile_and_run;

void Usage(char* arg)
{
    std::cout << "Usage:" << arg << " port" << std::endl;
}

int main(int argc, char* argv[])
{
    if(argc != 2) 
    {
        Usage(argv[0]);
        return -1;
    }

    Server svr;

    svr.Post("/Start", [](const Request &req, Response &resp)
             {
                 std::string in_json;
                 in_json = req.body;
                 if (!in_json.empty()) // 正文不为空，则处理
                 {
                     std::string out_json;
                     compile_and_run::Start(in_json, out_json);
                     resp.set_content(out_json, "application/json; charset=utf-8");
                 }
             });

    svr.listen("0.0.0.0", atoi(argv[1]));

    // Json::Value in_value;
    // in_value["code"] = R"(#include <iostream>
    // int main()
    // {
    //     int* p = new int[1024*1024*1024];
    //     std::cout << "哈哈哈哈哈哈" << std::endl;
    //     return 0;
    // })";
    // in_value["input"] = "";
    // in_value["cpuLimit"] = 1;
    // in_value["memLimit"] = 1024*30;

    // Json::FastWriter fast;
    // std::string in_json = fast.write(in_value);
    // std::string out_json;
    // // std::cout << in_json << std::endl;

    // wjh_compile_and_run::compile_and_run::Start(in_json, out_json);

    // Json::Reader Read;
    // Json::Value out_value;
    // Read.parse(out_json, out_value);

    // std::cout << out_value << std::endl;
    return 0;
}